home *** CD-ROM | disk | FTP | other *** search
/ MacFormat 1995 May / macformat-024.iso / Shareware City / Developers / TransSkel Pascal 2.5 / TransEdit / FakeAlert.p next >
Encoding:
Text File  |  1994-12-12  |  4.9 KB  |  172 lines  |  [TEXT/PJMM]

  1. {    In-memory item list for dialog with four items:}
  2.  
  3. {    1    "^0^1^2^3" (static text)}
  4. {    2    Button 1}
  5. {    3    Button 2}
  6. {    4    Button 3}
  7.  
  8. {    The caller of FakeAlert passes the four strings that are to be}
  9. {    substituted into the first item, the number of buttons that}
  10. {    should be used, and the titles to put into each button.}
  11. {    A copy of the item list is hacked to use the right number of}
  12. {    buttons.}
  13.  
  14. {    Thanks to Erik Kilk and Jason Haines.  Some of the stuff to do}
  15. {    this is modified from code they wrote.}
  16.  
  17. {    Ported to LightSpeed Pascal 8 January 1987 Owen Hartnett    }
  18.  
  19. unit FakeAlert;
  20.  
  21. interface
  22. {$IFC UNDEFINED THINK_PASCAL}
  23.     uses
  24.         Memtypes, Quickdraw, OSIntf, ToolIntf, PackIntf;
  25. {$ENDC}
  26.  
  27.     function FakeAlert (s1, s2, s3, s4: Str255; nButtons, defButton: integer; t1, t2, t3: Str255): integer;
  28. implementation
  29.  
  30.     var
  31.         ItemList: array[0..32] of integer;
  32.  
  33.         savePort: GrafPtr;
  34.         theDialog: DialogPtr;
  35.         iListHandle: Handle;
  36.         bounds: Rect;
  37.         itemHit: integer;
  38.  
  39.     procedure InitItemList;
  40.  
  41. {This proc performs static initializations on ItemList    }
  42.  
  43.     begin
  44.         Itemlist[0] := 3;                    { max number of items - 1 }
  45.         Itemlist[1] := 0;                    {    statText item}
  46.                                         { reserve a long for item handle }
  47.         itemlist[2] := 0;                    { display rectangle }
  48.         Itemlist[3] := 10;
  49.         Itemlist[4] := 27;
  50.         itemlist[5] := 61;
  51.         Itemlist[6] := 225;
  52.         Itemlist[7] := $8808;    { 8 + 128 = statText (disabled), title 8 bytes long }
  53.         itemlist[8] := $5e30;    { ^0^1^2^3 }
  54.         Itemlist[9] := $5e31;
  55.         Itemlist[10] := $5e32;
  56.         itemlist[11] := $5e33;
  57. {    first button}
  58.         Itemlist[12] := 0;                { reserve a long for item handle }
  59.         Itemlist[13] := 0;
  60.         itemlist[14] := 104;                { display rectangle }
  61.         Itemlist[15] := 140;
  62.         Itemlist[16] := 124;
  63.         itemlist[17] := 210;
  64.         Itemlist[18] := $400;        { 4 = pushButton, title is 0 bytes long}
  65. {    second button}
  66.         Itemlist[19] := 0;                { reserve a long for item handle }
  67.         itemlist[20] := 0;
  68.         Itemlist[21] := 104;                { display rectangle }
  69.         Itemlist[22] := 30;
  70.         itemlist[23] := 124;
  71.         Itemlist[24] := 100;
  72.         Itemlist[25] := $400;        { 4 = pushButton, title is 0 bytes long}
  73. {    third button}
  74.         itemlist[26] := 0;                { reserve a long for item handle }
  75.         Itemlist[27] := 0;
  76.         Itemlist[28] := 72;                { display rectangle }
  77.         itemlist[29] := 30;
  78.         Itemlist[30] := 92;
  79.         Itemlist[31] := 100;
  80.         itemlist[32] := $400;        { 4 = pushButton, title is 0 bytes long}
  81.     end;
  82.  
  83. {    Set dialog button title and draw bold outline if makeBold true.}
  84. {    This must be done after the window is shown or else the bold}
  85. {    outline won't show up (which is probably the wrong way to do it).}
  86.  
  87.     procedure SetDControl (theDialog: DialogPtr; itemNo: integer; title: Str255; makeBold: Boolean);
  88.         var
  89.             itemHandle: Handle;
  90.             itemType: integer;
  91.             itemRect: Rect;
  92.             pState: PenState;
  93.  
  94.     begin
  95.         GetDItem(theDialog, itemNo, itemType, itemHandle, itemRect);
  96.         SetCTitle(ControlHandle(itemHandle), title);
  97.         if makeBold then
  98.             begin
  99.                 GetPenState(pState);
  100.                 PenNormal;
  101.                 PenSize(3, 3);
  102.                 InsetRect(itemRect, -4, -4);
  103.                 FrameRoundRect(itemRect, 16, 16);
  104.                 SetPenState(pState);
  105.             end;
  106.     end;
  107.  
  108. {    Fake an alert, using an in-memory window and item list.}
  109. {    The message to be presented is constructed from the first}
  110. {    four arguments.  nButtons is the number of buttons to use,}
  111. {    defButton is the default button, the next three args are}
  112. {    the titles to put into the buttons.  The return value is}
  113. {    the button number (1..nButtons).  This must be interpreted}
  114. {    by the caller, since the buttons may be given arbitrary}
  115. {    titles.}
  116.  
  117. {    nButtons should be between 1 and 3, inclusive.}
  118. {    defButton should be between 1 and nButtons, inclusive.}
  119.  
  120.     function FakeAlert;
  121.         var
  122.             savePort: GrafPtr;
  123.             theDialog: DialogPtr;
  124.             iListHandle: Handle;
  125.             bounds: Rect;
  126.             itemHit: integer;
  127.  
  128.     begin
  129.         InitItemList;
  130.         InitCursor;
  131.         GetPort(savePort);
  132.         iListHandle := NewHandle(longint(512));
  133.         HLock(iListHandle);
  134.         ItemList[0] := nButtons;                                { = number items - 1 }
  135.         BlockMove(@ItemList[0], iListHandle^, longint(512));
  136.         SetRect(bounds, 115, 80, 355, 220);
  137.         theDialog := NewDialog(nil, bounds, '', false, dBoxProc, WindowPtr(-1), false, longint(0), iListHandle);
  138.         ParamText(s1, s2, s3, s4);                            { construct message }
  139.         SetPort(theDialog);
  140.         ShowWindow(theDialog);
  141.  
  142.         case nButtons of                { set button titles }
  143.             3: 
  144.                 begin
  145.                     SetDControl(theDialog, 4, t3, defButton = 3);
  146.                     SetDControl(theDialog, 3, t2, defButton = 2);
  147.                     SetDControl(theDialog, 2, t1, defButton = 1);
  148.                 end;
  149.             2: 
  150.                 begin
  151.                     SetDControl(theDialog, 3, t2, defButton = 2);
  152.                     SetDControl(theDialog, 2, t1, defButton = 1);
  153.                 end;
  154.             1: 
  155.                 SetDControl(theDialog, 2, t1, defButton = 1);
  156.         end;
  157.  
  158. {    ModalDialog returns 1 if return/enter hit, which, since}
  159. {    the statText item is first, can be unambiguously}
  160. {    interpreted as "choose default".}
  161.  
  162.         ModalDialog(nil, itemHit);
  163.         if itemHit = 1 then
  164.             itemHit := defButton
  165.         else
  166.             itemHit := itemHit - 1;
  167.         HUnlock(iListHandle);
  168.         DisposDialog(theDialog);
  169.         SetPort(savePort);
  170.         FakeAlert := itemHit;
  171.     end;
  172. end.